﻿<template>
  <a-spin :spinning="loading">
    <div class="wms-in-bound-page">
      <div class="in-bound-page-block block-one">
        <search-box 
          label="托盘号" 
          v-model="prevFrom.containter" 
          :msg-type="msgType.containter"
          :msg="msg.containter"
          :label-width="labelWidth" 
          placeholder="请输入..."
          @search="onContainterSearch" 
          @reset="onContainterReset"
        />
      </div>
      <div class="in-bound-page-block block-two">
        <search-box 
          label="物料编号" 
          v-model="prevFrom.material" 
          :msg-type="msgType.material"
          :msg="msg.material"
          :label-width="labelWidth" 
          placeholder="请输入..." 
          :btn-disabled="materialDisabled"
          @search="onMaterialSearch"
          @reset="onMaterialReset"
        />
        <div class="table-box">
          <a-table :columns="columns" :data-source="list" :row-key="tableKey" :pagination="false">
            <template slot="index" slot-scope="text, record, index">{{index+1}}</template>
            <template slot="input" slot-scope="text, record">
              <a-input-number v-model="record.bindquantity" :min="0" />
            </template>
            <template slot="action" slot-scope="text, record, index">
              <a-button type="danger" @click.stop="onDel(record,index)" :disabled="materialDisabled">删 除</a-button>
            </template>
          </a-table>
        </div>
      </div>
      <div class="in-bound-page-block block-three">
        <a-row :gutter="16">
          <a-col :md="12" :sm="24">
            <input-layout label="入库口" :label-width="labelWidth">
              <a-select v-model="sufFrom.entrance" style="width:100%;" allowClear>
                <a-select-option v-for="(item,index) in selectList.entrances" :value="item" :key="'entrance-sel-'+index">{{item}}</a-select-option>
              </a-select>
            </input-layout>
          </a-col>
          <a-col :md="12" :sm="24">
            <input-layout label="库位号" :label-width="labelWidth">
              <a-input placeholder="请输入..." v-model.trim="sufFrom.placecode" allowClear />
            </input-layout>
          </a-col>
        </a-row>
      </div>
      <div class="bottom-btns-view">
        <a-button type="primary" :disabled="bindBtnDisabled" @click="onBind">组盘</a-button>
        <a-button type="danger" :disabled="manualIntranceBtnDisabled" @click="onManualIntrance">人工入库</a-button>
        <a-button type="primary" :disabled="autoIntranceBtnDisabled" @click="onAutoIntrance">自动入库</a-button>
        <a-button type="danger" :disabled="moveBtnDisabled" @click="onTransfer">移库</a-button>
        <a-button @click="onReset">重 置</a-button>
      </div>
    </div>
  </a-spin>
</template>
<script>
import SearchBox from './searchBox.vue'
import InputLayout from './inputLayout.vue'
import { 
  BindEntranceGetContainer, 
  BindEntranceGetMaterial, 
  BindAction, 
  BindEntranceGetSelects, 
  BindEntranceManualWare, 
  BindEntranceAutoWare,
  BindEntranceTransfer
} from '@/api/modular/main/BindEntranceManage'
export default {
  name:'wmsBindEntrancePage',
  components:{SearchBox,InputLayout},
  data(){
    return {
      loading:false,
      labelWidth:'75px',
      containter:null,
      materialDisabled:false,
      msg:{
        containter:'',
        material:''
      },
      msgType:{
        containter:'info',
        material:'error'
      },
      prevFrom:{
        containter:'',
        material:''
      },
      sufFrom:{
        entrance:'',
        placecode:''
      },
      selectList:{
        entrances:[]
      },
      tableKey:'id',
      list:[],
      columns:[
        {
          dataIndex:'index',
          title:'序号',
          fixed:'left',
          width:'60',
          scopedSlots: { customRender: 'index' }
        },
        {
          dataIndex:'materialno',
          title:'物料编号'
        },
        {
          dataIndex:'materialname',
          title:'物料名称'
        },
        {
          dataIndex:'materialbatch',
          title:'批次号'
        },
        {
          dataIndex:'bindquantity',
          title:'数量',
          scopedSlots: { customRender: 'input' }
        },
        {
          dataIndex:'action',
          title:'操作',
          fixed:'right',
          width:'70',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  computed:{
    bindBtnDisabled(){
      let res = true;
      if (this.containter && this.containter.containerstatus!==3 && this.list.length>0) {
        res = false;
      }
      return res;
    },
    manualIntranceBtnDisabled(){
      let res = true;
      if (this.containter && this.containter.containerstatus!==3 && this.sufFrom.placecode) {
        res = false;
      }
      return res;
    },
    autoIntranceBtnDisabled(){
      let res = true;
      if (this.containter && this.containter.containerstatus!==3 && this.sufFrom.entrance) {
        res = false;
      }
      return res;
    },
    moveBtnDisabled(){
      let res = true;
      if (this.sufFrom.placecode && this.materialDisabled) {
        res = false;
      }
      return res;
    }
  },
  methods:{
    getSelectList(){
      this.loading = true;
      BindEntranceGetSelects().then((d)=>{
        this.selectList.entrances = d.data
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },
    onContainterReset(){
      this.resetContainter()
    },
    onContainterSearch(){
      if (!this.prevFrom.containter) {
        this.msgType.containter = 'error'
        this.msg.containter = '请输入托盘号！';
        return false;
      }
      
      this.msg.containter = ''
      this.loading = true;
      this.getContainterInfo((f)=>{
        this.loading = false;
      })
    },
    onMaterialReset(){
      this.resetMaterial()
    },
    resetContainter(){
      this.msg.containter = '';
      this.prevFrom.containter = ''
      this.containter = null;
      this.materialDisabled = false;
    },
    resetMaterial(){
      this.msg.material = '';
      this.prevFrom.material = ''
    },
    resetSufFrom(){
      this.sufFrom.entrance = ''
      this.sufFrom.placecode = ''
    },
    onMaterialSearch(){
      if (!this.prevFrom.material) {
        this.msg.material = '请输入物料编号！';
        return false;
      }
      this.msg.material = ''
      this.loading = true;
      this.getMaterialInfo((f)=>{
        this.loading = false;
      })
    },
    getContainterInfo(callback){
      let params = {Containercode:this.prevFrom.containter}
      BindEntranceGetContainer(params).then((d)=>{
        if (d.data) {
          let _temp1 = this.$options.filters['dictType']('container_type',d.data.wmsContainer.containertype)
          let _temp2 = this.$options.filters['dictType']('container_status',d.data.wmsContainer.containerstatus)
          this.msgType.containter = 'info'
          this.msg.containter = `材质：${_temp1}，状态：${_temp2}`;
          this.containter = d.data.wmsContainer
          if (d.data.wmsContainer.containerstatus===3) {
            this.materialDisabled = true;
          } else {
						this.materialDisabled = false;
					}
          if (d.data.wmsMaterials) {
            this.list = d.data.wmsMaterials
          } 
           callback && callback(true)
        } else {
          this.msgType.containter = 'error'
          this.msg.containter = d.message || '查无信息！';
          this.containter = null;
          this.list = []
          callback && callback(false)
        }
      }).catch((err)=>{
        this.containter = null;
        this.list = []
        callback && callback(false)
      })
    },
    getMaterialInfo(callback){
      let params = {Materialno:this.prevFrom.material}
      BindEntranceGetMaterial(params).then((d)=>{
        if (d.data) {
          let cf = this.checkMaterialSame(d.data);
          if (cf) {
            let obj = {...d.data}
            obj.bindquantity = 0;
            this.list.push(obj)
            callback && callback(true)
          } else {
            callback && callback(false)
          } 
        } else {
          this.msg.material = d.message || '查无信息！';
          callback && callback(false)
        }
      }).catch((err)=>{
        callback && callback(false)
      })
    },
    checkMaterialSame(obj) {
      let res = true;
      for (let i=0;i<this.list.length;i++) {
        if (this.list[i].materialno===obj.materialno) {
          res = false;
          break;
        }
      }
      if (!res) {
        this.$error({
          title: '系统提示',
          content: '已有物料，不可添加！',
        });
      }
      return res;
    },
    getContainterMaterials(callback){
      let params = {ContainerName:this.prevFrom.containter}
      WmsGetContainterList(params).then(d=>{
        this.list = d.data || []
        callback && callback(true)
      }).catch(err=>{
        this.list = []
        callback && callback(false)
      })
    },
    onDel(obj,index){
      this.list.splice(index,1)
    },
    onManualIntrance(){
      let cf = this.checkBeforeSubmit();
      if (!cf) return false;
      this.loading = true;
      this.dealManualIntrance((f)=>{
        this.loading = false;
        if (f) {
          this.$message.success('入库成功！');
          this.resetContainter()
          this.resetMaterial()
          this.resetSufFrom()
          this.list = [];
        }
      })
    },
    onAutoIntrance(){
      let cf = this.checkBeforeSubmit();
      if (!cf) return false;
      this.loading = true;
      this.dealAutoIntrance((f)=>{
        this.loading = false;
        if (f) {
          this.$message.success('入库成功！');
          this.resetContainter()
          this.resetMaterial()
          this.resetSufFrom()
          this.list = [];
        }
      })
    },
    onBind(){
      let cf = this.checkBeforeSubmit('bind');
      if (!cf) return false;
      this.loading = true;
      this.dealBind((f)=>{
        if (f) {
          this.getContainterInfo((f1)=>{
            this.loading = false;
            this.$message.success('组盘成功！');
          })
        } else {
          this.loading = false;
        }
      })
    },
    onTransfer(){
      this.loading = true;
      this.dealTransfer((f)=>{
        this.loading = false;
        if (f) {
          this.$message.success('移库成功！');
          this.resetContainter()
          this.resetMaterial()
          this.resetSufFrom()
          this.list = [];
        }
      })
    },
    onReset(){
      this.resetContainter()
      this.resetMaterial()
      this.list = [];
    },
    checkBeforeSubmit(type){
      let flag=true, msg="";
      
      if (this.list.length>0) {
        for (let i=0;i<this.list.length;i++) {
          if (!this.list[i].bindquantity) {
            msg = `请确认第${i+1}笔物料的数量！`
            flag = false;
          }
          if (flag && this.list[i].bindquantity!==parseInt(this.list[i].bindquantity)) {
             msg = `第${i+1}笔物料,数量只能是正整数！`
             flag = false;
          }
        }
      } else {
        if (type==='bind') {
          msg = '没有物料信息！'
          flag = false;
        }
      }
      
      if (!flag && msg) {
        this.$error({
          title: '系统提示',
          content: msg,
        });
      }
      
      return flag;
    },
    dealBind(callback){
      let params = {
        containercode:this.containter.containercode,
        wmsMaterials:this.list
      }
      BindAction(params).then(()=>{
        callback && callback(true)
      }).catch(()=>{
        callback && callback(false)
      })
    },
    dealManualIntrance(callback){
      let params = {
        containercode:this.containter.containercode,
        wmsMaterials:this.list,
        placecode:this.sufFrom.placecode
      }
      BindEntranceManualWare(params).then(()=>{
        callback && callback(true)
      }).catch(()=>{
        callback && callback(false)
      })
    },
    dealAutoIntrance(callback){
      let params = {
        containercode:this.containter.containercode,
        wmsMaterials:this.list,
        placecode:this.sufFrom.placecode,
        entrance:this.sufFrom.entrance
      }
      BindEntranceAutoWare(params).then(()=>{
        callback && callback(true)
      }).catch(()=>{
        callback && callback(false)
      })
    },
    dealTransfer(callback){
      let params = {
        containercode:this.containter.containercode,
        placecode:this.sufFrom.placecode
      }
      BindEntranceTransfer(params).then(()=>{
        callback && callback(true)
      }).catch(()=>{
        callback && callback(false)
      })
    }
  },
  mounted(){
    this.getSelectList()
  }
}
</script>
<style lang="less" scoped>
.wms-in-bound-page{
  border-radius: 6px 6px 0 0;
  background-color: #fff;
}
.in-bound-page-block{
  border-bottom:2px solid #dcdcdc;
  overflow: auto;
  padding-top: 16px;
  padding-bottom: 16px;
  &.block-two{
    .table-box{
      padding-top:20px;
      min-height: 241px;
    }
  }
  &.block-three{
     overflow: hidden;
     padding-left:8px;
     padding-right:8px;
  }
}
.bottom-btns-view{
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  padding:16px 8px;
  .ant-btn + .ant-btn {
    margin-left:20px;
  }
}
</style>
